<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>Map Constructors &amp; Destructors</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Maps</a> &gt; <a href=
    "map_constructors.html">Map Constructors &amp;
    Destructors</a>
  </div>

  <div class="name-format">
    Map Constructors &amp; Destructors
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;map&gt;
  map();
  map( const map&amp; m );
  map( iterator start, iterator end );
  map( iterator start, iterator end, const key_compare&amp; cmp );
  map( const key_compare&amp; cmp );
  ~map();
</pre>

  <p>The default constructor takes no arguments, creates a new instance
  of that map, and runs in <a href="../complexity.html">constant
  time</a>. The default copy constructor runs in <a href=
  "../complexity.html">linear time</a> and can be used to create a new
  map that is a copy of the given map <em>m</em>.</p>

  <p>You can also create a map that will contain a copy of the
  elements between <em>start</em> and <em>end</em>, or specify a
  comparison function <em>cmp</em>.

  <p>The default destructor is called when the map should be
  destroyed.</p>

  <p>For example, the following code creates a map that associates a
  string with an integer:</p>
  <pre class="example-code">
  struct strCmp {
    bool operator()( const char* s1, const char* s2 ) const {
      return strcmp( s1, s2 ) &lt; 0;
    }
  };

  ...

  map&lt;const char*, int, strCmp&gt; ages;
  ages["Homer"] = 38;
  ages["Marge"] = 37;
  ages["Lisa"] = 8;
  ages["Maggie"] = 1;
  ages["Bart"] = 11;

  cout &lt;&lt; "Bart is " &lt;&lt; ages["Bart"] &lt;&lt; " years old" &lt;&lt; endl;
</pre>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="map_operators.html">Map Operators</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
